<template>
  <div>
    <m-form
      ref="form"
      label-width="100px"
      :options="options"
      @on-preview="handlePreview"
      @on-remove="handleRemove"
      @on-exceed="handleExceed"
      @on-success="handleSuccess"
      @before-remove="beforeRemove"
    >
      <template #uploadArea>
        <el-button type="primary">Click to upload</el-button>
      </template>
      <template #uploadTip>
        <div class="el-upload__tip">
          jpg/png files with a size less than 500KB.
        </div>
      </template>
      <!-- 子组件传过来的作用域 -->
      <template #action="scope">
        <el-button type="primary" @click="submitForm(scope)">提交</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </template>
    </m-form>
  </div>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { FormInstance, FormOptions } from "../../components/form/src/type/type";
interface Scope {
  form: FormInstance;
  model: any;
}
let options: FormOptions[] = [
  {
    type: "input",
    value: "",
    label: "用户名",
    prop: "username",
    rules: [
      {
        required: true,
        message: "用户名不能为空",
        trigger: "blur",
      },
      {
        min: 2,
        max: 6,
        required: true,
        message: "用户名2-6位之间",
        trigger: "blur",
      },
    ],
    attrs: {
      // showPassWord: true,
    },
  },
  {
    type: "input",
    value: "",
    label: "密码",
    prop: "password",
    rules: [
      {
        required: true,
        message: "密码不能为空",
        trigger: "blur",
      },
      {
        min: 6,
        max: 12,
        required: true,
        message: "密码6-12位之间",
        trigger: "blur",
      },
    ],
    attrs: {
      showPassWord: false,
    },
  },
  {
    type: "select",
    value: "",
    label: "职位",
    placeholder: "请选择",
    prop: "role",
    rules: [
      {
        required: true,
        message: "职位不能为空",
        trigger: "blur ",
      },
    ],
    attrs: {
      style: {
        width: "100%",
      },
      clearable: true,
    },
    children: [
      {
        type: "option",
        label: "经理",
        value: "1",
        attrs: {},
      },
      {
        type: "option",
        label: "主管",
        value: "2",
        attrs: {},
      },
    ],
  },
  {
    type: "upload",
    label: "上传",
    prop: "pic",
    rules: [
      {
        required: true,
        message: "文件不能为空",
        trigger: "blur ",
      },
    ],
    uploadAttrs: {
      action: "https://jsonplaceholder.typicode.com/posts/",
      multiple: true,
      limit: 3,
    },
  },
  {
    type: "editor",
    value: "<p>我是一条描述内容</p>",
    prop: "desc",
    placeholder: "请输入内容",
    label: "描述",
    // rules: [ { required: true, message: '描述不能为空', trigger: 'blur'}]
  },
];

let form = ref();

const submitForm = (scope: Scope) => {
  scope.form.validate((valid) => {
    if (valid) {
      console.log(scope.model);
      ElMessage.success("提交成功");
    } else {
      ElMessage.error("表单填写有误，请检查");
    }
  });
};
const resetForm = () => {
  form.value.resetFields();
};

// 图片上传事件
const handlePreview = (file: any) => {
  console.log(file);
};

const handleRemove = (val: any) => {
  console.log(val.file, val.fileList);
};

const handleExceed = (val: any) => {
  ElMessage.warning(
    `The limit is 3, you selected ${
      val.file.length
    } files this time, add up to ${
      val.file.length + val.fileList.length
    } totally`
  );
};

const beforeRemove = (val: any) => {
  return ElMessageBox.confirm(
    `Cancel the transfert of ${val.file.name} ?`
  ).then(
    () => true,
    () => false
  );
};

const handleSuccess = (val: any) => {
  console.log("success", val);
};
</script>
<style lang="scss" scoped></style>
